﻿<?php
include_once(dirname(__FILE__).'/../lib/Db.class.php');
include_once(dirname(__FILE__).'/Product.class.php');
class T2Factory implements ProductFactory{
    private $db;

    function __construct(){
        $this->db = Db::getInstance();
    }
    public function createProduct($product_id){
        $t1_id = $this->getT1Id($product_id);
        // 查询第一部分的消耗量
        $sql = "SELECT materialTypeID as id,text as name,quantity-coalesce((
            select quantity from invtypematerials where typeID = ".$t1_id." 
            and id = materialTypeID),0) as quantity FROM invtypematerials,trntranslations 
            where typeID = ".$product_id." and materialTypeID = keyID and languageID = 'zh' 
            and tcID = (select tcID from trntranslationcolumns where columnName = 'typeName');";

        $getMeaterialsPartOne = function($query_result){
            $materials = array();
            if(is_array($query_result))
            {
                foreach($query_result as $row)
                {
                    if($row->quantity > 0)
                    {
                        $material = array('id'=>$row->id,
                            'name'=>$row->name,
                            'quantity'=>$row->quantity,
                            'damage_per_job'=>1);
                        array_push($materials,$material);
                    }
                }
            }
            return $materials;
        };

        $part_one = $this->db->db_query($sql,$getMeaterialsPartOne);

        $t2_blueprint_id = $this->getBluePrintId($product_id);
        // 查询第二部分的消耗量
        $sql = "SELECT requiredTypeID as id,text as name,quantity,damagePerJob 
            FROM ramtyperequirements,trntranslations where typeID = ".$t2_blueprint_id." 
            and activityID = 1 and damagePerJob != 0 and requiredTypeID = keyID 
            and languageID = 'zh' and tcID = (select tcID from trntranslationcolumns 
            where columnName = 'typeName');"; 

        $getMeaterialsPartTwo = function($query_result){
            $materials = array();
            if(is_array($query_result))
            {
                foreach($query_result as $row)
                {
                    $material = array('id'=>$row->id,
                        'name'=>$row->name,
                        'quantity'=>$row->quantity,
                        'damage_per_job'=>$row->damagePerJob);
                    array_push($materials,$material);
                }
            }
            return $materials;
        };

        $part_two = $this->db->db_query($sql,$getMeaterialsPartTwo);

        $materials = array_merge($part_one,$part_two);
        $product = new Product($product_id,$materials);
        return $product;
    }
    private function getT1Id($id){
        $sql = "SELECT parentTypeID FROM eve.invmetatypes 
            where typeID = ".$id." and metaGroupID = 2;";

        $t1_id = $this->db->db_query($sql,create_function('$query_result',
            'return $query_result[0]->parentTypeID;'));
        return $t1_id;
    }
    private function getBluePrintId($id){
        $sql = "SELECT blueprintTypeID FROM eve.invblueprinttypes 
            where productTypeID = ".$id.";";
        $blueprint_id = $this->db->db_query($sql,create_function('$query_result',
            'return $query_result[0]->blueprintTypeID;'));
        return $blueprint_id;
    }
}

?>
